package Text17;

import java.io.*;

public class Main {
    //    输入一串数字，给你 M个询问，每次询问就给你两个数字 X,Y，要求你说出 X
//    到 Y这段区间内的最大数。
    static int N = 4000000;
    static Node[] w = new Node[N];
    static int[] arr = new int[N];

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        String[] s = br.readLine().split(" ");
        int n = Integer.parseInt(s[0]);
        int m = Integer.parseInt(s[1]);
        String[] str = br.readLine().split(" ");
        for (int i = 0; i < n; i++) {
            arr[i + 1] = Integer.parseInt(str[i]);
        }
        build(1, 1, n);
        while (m-- > 0) {
            String s1[] = br.readLine().split(" ");
            int x = Integer.parseInt(s1[0]);
            int y = Integer.parseInt(s1[1]);
            bw.write(query(1, x, y)+"\n");
        }
        bw.flush();
    }

    private static void pushup(int u) {
        w[u].max = Math.max(w[u << 1].max, w[u << 1 | 1].max);
    }

    private static void build(int u, int l, int r) {
        if (l == r) {
            w[u] = new Node(l, r, arr[l]);
        } else {
            w[u] = new Node(l, r);
            int mid = l + r >> 1;
            build(u << 1, l, mid);
            build(u << 1 | 1, mid + 1, r);
            pushup(u);
        }
    }

    private static int query(int u, int a, int b) {
        if (w[u].l >= a && w[u].r <= b) {
            return w[u].max;
        }
        int res = Integer.MIN_VALUE;
        int mid = w[u].l + w[u].r >> 1;
        if (mid >= a) {
            res = query(u << 1, a, b);
        }
        if (mid + 1 <= b) {
            res = Math.max(res, query(u << 1 | 1, a, b));
        }
        return res;
    }
}

class Node {
    int l, r, max;

    public Node(int l, int r, int max) {
        this.l = l;
        this.r = r;
        this.max = max;
    }

    public Node(int l, int r) {
        this.l = l;
        this.r = r;
    }
}